home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 009a / actlib10.zip / TOOLS.H < prev    next >
C/C++ Source or Header  |  1993-02-05  |  17KB  |  671 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #ifndef __Tools_H
  4. #define __Tools_H
  5.  
  6. #include <stdio.h>
  7. #include <io.h>
  8. #include <dos.h>
  9. #include "c2cpp.h"
  10.  
  11.  
  12. /* for Microsoft */
  13.  
  14. #ifndef MK_FP
  15. #define MK_FP( seg, ofs )   (void far *)( (unsigned long)((void far *)(seg)) + (unsigned long)((void near *)(ofs)) )
  16.  
  17. struct  fcb {
  18.     char    fcb_drive;      /* 0 = default, 1 = A, 2 = B */
  19.     char    fcb_name[8];    /* File name */
  20.     char    fcb_ext[3];     /* File extension */
  21.     short   fcb_curblk;     /* Current block number */
  22.     short   fcb_recsize;    /* Logical record size in bytes */
  23.     long    fcb_filsize;    /* File size in bytes */
  24.     short   fcb_date;       /* Date file was last written */
  25.     char    fcb_resv[10];   /* Reserved for DOS */
  26.     char    fcb_currec;     /* Current record in block */
  27.     long    fcb_random;     /* Random record number */
  28. };
  29.  
  30. struct  xfcb    {
  31.     char        xfcb_flag;  /* Contains 0xff to indicate xfcb */
  32.     char        xfcb_resv[5];/* Reserved for DOS */
  33.     char        xfcb_attr;  /* Search attribute */
  34.     struct  fcb xfcb_fcb;   /* The standard fcb */
  35. };
  36.  
  37. #endif
  38.  
  39.  
  40.             
  41. typedef unsigned char BYTE;
  42.  
  43.  
  44. /***
  45.  *  Function    :  HIBYTE/LOWBYTE
  46.  *
  47.  *  Description :  Extract high/low byte of an integer
  48.  *
  49.  *  Parameters  :  in   int x
  50.  *
  51.  *  Side-effects:  Macro, so be careful.
  52.  *
  53.  *  Return      :  none
  54.  ***/
  55.  
  56. #define HIBYTE( x ) ( (unsigned int)(x) >> 8 )
  57. #define LOWBYTE( x) ( (unsigned char)(x) )
  58.  
  59.  
  60. /***
  61.  *  Function    :  CAST
  62.  *
  63.  *  Description :  Cast any object to any type.
  64.  *
  65.  *  Parameters  :  in     new_type      any type
  66.  *                 in     object        any variable
  67.  *
  68.  *  Side-effects:  Macro, so be careful.
  69.  *
  70.  *  Return      :  none
  71.  ***/
  72.  
  73. #define CAST( new_type, object ) ( *((new_type *)&object) )
  74.      
  75.  
  76. /***
  77.  *  Function    :  SWAP
  78.  *
  79.  *  Description :  Swap two arguments of any type.
  80.  *
  81.  *  Parameters  :  in/out   x      any type argument
  82.  *                 in/out   y      any type argument
  83.  *
  84.  *  Side-effects:  Macro, so be careful.
  85.  *
  86.  *  Return      :  none
  87.  ***/
  88.  
  89. #define SWAP( x, y )        ( (x) ^= (y) ^= (x) ^= (y) )
  90.  
  91.  
  92. /***
  93.  *  Function    :  fileexist
  94.  *
  95.  *  Description :  Test if a filename exist
  96.  *
  97.  *  Parameters  :  in   char *filename
  98.  *
  99.  *  Return      :  0 or 1
  100.  ***/
  101.  
  102. #define fileexist( filename )   ( ! access(filename, 0) )
  103.  
  104.  
  105. /***
  106.  *  Function    :  ICA_get/put
  107.  *
  108.  *  Description :  Intra-application Communications Area get/put
  109.  *                 a 16 bytes data
  110.  *
  111.  *  Parameters  :  in   void *ptr   pointer to a 16 bytes data
  112.  *
  113.  *  Return      :  none
  114.  ***/
  115.  
  116. #define ICA_get( ptr )   ( memcpy(ptr, MK_FP(0x0040, 0x00F0), 16) )
  117. #define ICA_put( ptr )   ( memcpy(MK_FP(0x0040, 0x00F0), ptr, 16) )
  118.  
  119.  
  120.  
  121. /***
  122.  *  Function    :  getkey
  123.  *
  124.  *  Description :  Return a 2-bytes key pressed.
  125.  *
  126.  *  Parameters  :  none
  127.  *
  128.  *  Decisions   :  extended characters are added to 256.
  129.  *
  130.  *  Return code :  code of key pressed.
  131.  *
  132.  *  OS/Compiler :  MS-DOS
  133.  ***/
  134.  
  135. EXTERN int getkey( void );
  136.  
  137.  
  138. /***
  139.  *  Function    :  ungetkey
  140.  *
  141.  *  Description :  Puts a 2-bytes key into the keyboard buffer
  142.  *
  143.  *  Parameters  :  in    int key           key to be buffered
  144.  *
  145.  *  Decisions   :  extended characters are added to 256.
  146.  *
  147.  *  Return code :  none
  148.  *
  149.  *  Warning     :  May not work if a keyboard buffer extender is used.
  150.  *                 Does not handle the case of buffer full.
  151.  *
  152.  *  OS/Compiler :  MS-DOS
  153.  ***/
  154.  
  155. EXTERN void ungetkey( int key );
  156.  
  157.  
  158.  
  159. /***
  160.  *  Function    :  fnreduce
  161.  *
  162.  *  Description :  Transforms a filename into a full reduced filename.
  163.  *
  164.  *  Parameters  :  in/out   char *filename    input/reduced filename
  165.  *
  166.  *  Decisions   :  Translate into uppercase
  167.  *                  '/' are tranlated into '\\'
  168.  *
  169.  *  Return      :  pointer to result
  170.  *
  171.  *  OS/Compiler :  MS-DOS
  172.  ***/
  173.  
  174. EXTERN char *fnreduce( char *filename );
  175.  
  176.  
  177. /***
  178.  *  Function    :  getpgmpath
  179.  *
  180.  *  Description :  Transforms a filename into a full pathname
  181.  *                 relative to program directory.
  182.  *
  183.  *  Parameters  :  in/out   char *filename    input filename
  184.  *
  185.  *  Return      :  pointer to filename    
  186.  *
  187.  *  Precond     :  Global variable extern char **_argv must be defined
  188.  *                 and assign to argv in main( int argc, char *argv[] )
  189.  *                 Built-in in Borland
  190.  *
  191.  *  OS/Compiler :  MS-DOS version >= 3.0
  192.  ***/
  193.  
  194. EXTERN char *getpgmpath( char *filename );
  195.  
  196.  
  197. /***
  198.  *  Function    :  gettmppath
  199.  *
  200.  *  Description :  Transforms a filename into a full pathname
  201.  *                 relative to temporary directory.
  202.  *
  203.  *  Decisions   :  Temporary directory is first searched
  204.  *                 into environment variable 'TEMP',
  205.  *                 after into environment variable 'TMP'.
  206.  *                 If none are defined, program directory
  207.  *                 is assumed.
  208.  *
  209.  *  Parameters  :  in/out  char *filename    filename
  210.  *
  211.  *  Return      :  pointer to filename
  212.  *
  213.  *  Precond     :  Global variable extern char **_argv must be defined
  214.  *                 and assign to argv in main( int argc, char *argv[] )
  215.  *                 Built-in in Borland
  216.  *
  217.  *  OS/Compiler :  MS-DOS version >= 3.0
  218.  ***/
  219.  
  220. EXTERN char *gettmppath( char *filename );
  221.  
  222.  
  223. /***
  224.  *  Function    :  truename
  225.  *
  226.  *  Description :  Transforms a filename into a full reduced filename.
  227.  *                 Resolves all SUBST's, JOIN's, network names,...
  228.  *
  229.  *  Parameters  :  in/out   char *filename    input/reduced filename
  230.  *
  231.  *  Warning     :   - Valid only for DOS >= 2.0
  232.  *                  - Trailing '\' is not removed
  233.  *
  234.  *  Return      :  0      OK
  235.  *                 2  Invalid syntax
  236.  *                 3  Invalid drive or path
  237.  *
  238.  *  OS/Compiler :  MS-DOS version >= 2.0
  239.  ***/
  240.  
  241. EXTERN int truename( char *filename );
  242.  
  243.  
  244. /***
  245.  *  Function    :  exthandles
  246.  *
  247.  *  Description :  Allows MS-DOS program to open 255 handles
  248.  *
  249.  *  Parameters  :  none
  250.  *
  251.  *  Return      :  none
  252.  *
  253.  *  OS/Compiler :  MS-DOS version >= 3.30
  254.  ***/
  255.  
  256. EXTERN void exthandles( void );
  257.  
  258.  
  259.  
  260. /***
  261.  *  Function    :  getdensity
  262.  *
  263.  *  Description :  Return the density of a floppy drive.
  264.  *
  265.  *  Parameters  :  in    int drive         0 = A:, 1 = B:
  266.  *
  267.  *  Return      :  density in Kb (360, 720, 1200, 1440)
  268.  *                 0 if invalid drive or drive not ready
  269.  *
  270.  *  OS/Compiler :  MS-DOS
  271.  ***/
  272.  
  273. EXTERN int getdensity( int drive );
  274.  
  275.  
  276. /***
  277.  *
  278.  *  Function    :  is_drive_ready
  279.  *
  280.  *  Description :  Return if a floppy drive is ready or not.
  281.  *
  282.  *  Parameters  :  in    int drive         0 = A:, 1 = B:, ...
  283.  *
  284.  *  Return      :  0 if not ready (or invalid)
  285.  *                 1 if ready
  286.  *
  287.  *  OS/Compiler :  MS-DOS
  288.  ***/
  289.  
  290. EXTERN int is_drive_ready( int drive );
  291.  
  292.  
  293.  
  294. /***
  295.  *
  296.  *  Function    :  geterrorlevel
  297.  *
  298.  *  Description :  Return errorlevel code from previous command executed
  299.  *                 by top COMMAND.COM
  300.  *
  301.  *  Parameters  :  none
  302.  *
  303.  *  Return      :  errorlevel
  304.  *                 -1 If MS-DOS version not supported
  305.  *
  306.  *  Warning     :  This function is only valid with COMMAND.COM from
  307.  *                 MS-DOS 3.20, 3.21, 3.30, 4.0, 4.01, 5.0 & NDOS 6.0.
  308.  */
  309.  
  310. EXTERN int geterrorlevel( void );
  311.  
  312.  
  313.  
  314. /***
  315.  *  Function    :  getdiskfree
  316.  *
  317.  *  Description :  Return the number of bytes free of a disk.
  318.  *
  319.  *  Parameters  :  in    int disk         0 = A:, 1 = B:, ...
  320.  *
  321.  *  Return      :  number of bytes free
  322.  *                 -1 if invalid drive
  323.  *
  324.  *  OS/Compiler :  MS-DOS
  325.  ***/
  326.  
  327. EXTERN long getdiskfree( int disk );
  328.  
  329.  
  330. /***
  331.  *  Function    :  setcwd
  332.  *
  333.  *  Description :  Change to specified directory accross disks
  334.  *
  335.  *  Parameters  :  in    char *dir    input directory name
  336.  *
  337.  *  Return      :  0      OK
  338.  *                 other  Invalid syntax, drive or path
  339.  *
  340.  *  OS/Compiler :  MS-DOS
  341.  ***/
  342.  
  343. EXTERN int setcwd( const char *dir );
  344.  
  345.  
  346. /***
  347.  *  Function    :  getsetup
  348.  *
  349.  *  Description :  Read paramaters in setup file.
  350.  *
  351.  *  Parameters  :  in    FILE *file        input file
  352.  *                 in    char *format      "keyword1=format keyword2=format"
  353.  *                 out   pointer list
  354.  *
  355.  *  Decisions   :  Expected format is a list of lines of the form
  356.  *
  357.  *                                 <keyword>=<value>
  358.  *
  359.  *                 - All letters are treated (and returned) as uppercases.
  360.  *                 - Blanks and tabs are skipped.
  361.  *
  362.  *                 - <value> can be enclosed between double quotes
  363.  *                    to preserve lowercases and blanks.
  364.  *
  365.  *                   - 2 consecutive double quotes allow to keep
  366.  *                   a double quote in <value>.
  367.  *
  368.  *                 - A number sign (#) in first column forces the line
  369.  *                   to be ignored.
  370.  *
  371.  *                 - The input formats in parameter 'format' are C standard
  372.  *                   ones ( %d, %s, %f,...)
  373.  *
  374.  *  Return      :   0     OK
  375.  *                 -1     keyword not found
  376.  *                 -2     file problem
  377.  *
  378.  *  Precond     :  Input file must be opened with read access.
  379.  *
  380.  *  OS/Compiler :  All
  381.  */
  382.  
  383. EXTERN int getsetup( FILE *file, const char *format, ... );
  384.  
  385.  
  386. /***
  387.  *  Function    :  load_options
  388.  *
  389.  *  Description :  Load options from a file.
  390.  *
  391.  *  Parameters  :  out  void * options     ptr to variable containing options
  392.  *                 in   size_t size        size of data to read
  393.  *
  394.  *  Decisions   :  The file has the same name as the program
  395.  *                 with extension .OPT and is searched in
  396.  *                 current directory, then in program directory.
  397.  *                 If option file is not found, options are not modified.
  398.  *
  399.  *  Return      :  number of bytes read
  400.  *                 -1 if file was not found
  401.  *
  402.  *  Precond     :  Global variable extern char **_argv must be defined
  403.  *                 and assign to argv in main( int argc, char *argv[] )
  404.  *                 Built-in in Borland
  405.  *
  406.  *  OS/Compiler :  MS-DOS version >= 3.0
  407.  ***/
  408.  
  409. EXTERN int load_options( void *options, size_t size );
  410.  
  411. /***   To not specify the size   ***/
  412. #define Opt_load( opt )     load_options( (opt), sizeof(*(opt)) )
  413.  
  414.  
  415.  
  416.  
  417. /***
  418.  *  Function    :  save_options
  419.  *
  420.  *  Description :  Save options in a file.
  421.  *
  422.  *  Parameters  :  in  void * options     ptr to variable containing options
  423.  *                 in  size_t size        size of data to read
  424.  *
  425.  *  Decisions   :  If options were previously loaded, this file is used.
  426.  *                 Otherwise, the file has the same name as the program
  427.  *                 with extension .OPT and is located in program directory.
  428.  *
  429.  *  Return      :  number of bytes written
  430.  *                 -1 if file not opened
  431.  *
  432.  *  Precond     :  Global variable extern char **_argv must be defined
  433.  *                 and assign to argv in main( int argc, char *argv[] )
  434.  *                 Built-in in Borland
  435.  *
  436.  *  OS/Compiler :  MS-DOS version >= 3.0
  437.  ***/
  438.  
  439. EXTERN int save_options( const void *options, size_t size );
  440.  
  441. /***   To not specify the size   ***/
  442. #define Opt_save( opt )     save_options( (opt), sizeof(*(opt)) )
  443.  
  444.  
  445. /***
  446.  *
  447.  *  Function    :  load_cfg
  448.  *
  449.  *  Description :  Load configuration from a file.
  450.  *
  451.  *  Parameters :   in   char * name        filename
  452.  *                 out  void * config      ptr to variable containing configuration
  453.  *                 in   size_t size        size of data to read
  454.  *
  455.  *  Decisions   :  The file has the same name as the parameter 'name'
  456.  *                 but with extension .CFG
  457.  *                 If configuration file is not found, config is unchanged.
  458.  *
  459.  *  Return     :   number of bytes read
  460.  *                 -1 if file was not found
  461.  *
  462.  *  OS/Compiler :  MS-DOS
  463.  ***/
  464.  
  465. EXTERN int load_cfg( const char *name, void *config, size_t bsize );
  466.  
  467. /***   To not specify the size   ***/
  468. #define Cfg_load( name, cfg )   load_cfg( (name), (cfg), sizeof(*(cfg)) )
  469.  
  470.  
  471.  
  472. /***
  473.  *
  474.  *  Function    :  save_cfg
  475.  *
  476.  *  Description :  Save a configuration into a file.
  477.  *
  478.  *  Parameters  :  in   char * name        filename
  479.  *                 in   void * config      ptr to variable containing configuration
  480.  *                 in   size_t size        size of data to write
  481.  *
  482.  *  Decisions   :  The file has the same name as the parameter 'name'
  483.  *                 but with extension .CFG
  484.  *
  485.  *  Return      :  number of bytes written
  486.  *                 -1 if file was not found
  487.  *
  488.  *  OS/Compiler :  MS-DOS
  489.  ***/
  490.  
  491. EXTERN int save_cfg( const char *name, const void *config, size_t bsize );
  492.  
  493. /***   To not specify the size   ***/
  494. #define Cfg_save( name, cfg )   save_cfg( (name), (cfg), sizeof(*(cfg)) )
  495.  
  496.  
  497.  
  498. /***
  499.  *  Function    :  filencopy
  500.  *
  501.  *  Description :  Copy n characters from a file on another.
  502.  *
  503.  *  Parameters  :  in   int            target    target file descriptor
  504.  *                 in   int            source    source file descriptor
  505.  *                 in   unsigned long  length    number of bytes to copy
  506.  *
  507.  *  Precond     :  The two files must be opened.
  508.  *
  509.  *  Decisions   :  The copy can stop when eof is reached.
  510.  *
  511.  *  Warning     :  no rewind is performed on either file!!!
  512.  *
  513.  *  Return      :  0 if OK
  514.  *                 errno if error
  515.  *
  516.  *  OS/Compiler :  All
  517.  ***/
  518.  
  519. EXTERN int filencopy( int source, int target, unsigned long length );
  520.  
  521. /* maximum size */
  522. #define filecopy( source, target )    filencopy( source, target, 0xFFFFFFFF )
  523.  
  524.  
  525. /***
  526.  *  Function    :  Filencopy
  527.  *
  528.  *  Description :  Copy n characters from a file on another.
  529.  *
  530.  *  Parameters  :  in   char *         source    source filename
  531.  *                 in   char *         target    target filename
  532.  *                 in   unsigned long  length    number of bytes to copy
  533.  *
  534.  *  Decisions   :  The copy can stop when eof is reached.
  535.  *
  536.  *  Return      :  0 if OK
  537.  *                 errno if error
  538.  *
  539.  *  OS/Compiler :  All
  540.  ***/
  541.  
  542. EXTERN int Filencopy( const char *source, const char *target, unsigned long length );
  543.  
  544. /* maximum size */
  545. #define Filecopy( source, target )    Filencopy( source, target, 0xFFFFFFFF )
  546.  
  547.  
  548.  
  549.  
  550. /***
  551.  *  Function    :  Filecat
  552.  *
  553.  *  Description :  Concatenate two files on another.
  554.  *
  555.  *  Parameters  :  in   char *     newpath     result filename
  556.  *                 in   char *     path1       target filename
  557.  *                 in   char *     path2       source filename
  558.  *
  559.  *  Return      :  0 if OK
  560.  *                 errno if error
  561.  *
  562.  *  OS/Compiler :  All
  563.  ***/
  564.  
  565. EXTERN int Filecat( const char *newpath, const char *path1, const char *path2 );
  566.  
  567.  
  568. /***
  569.  *  Function    :  filetrunc
  570.  *
  571.  *  Description :  Truncate a file to a specified length.
  572.  *
  573.  *  Parameters  :  in   int            fd        file descriptor
  574.  *                 in   unsigned long  length    number of bytes to copy
  575.  *
  576.  *  Precond     :  The file must be opened with writing permission
  577.  *
  578.  *  Warning     :  no rewind is performed on the file!!!
  579.  *
  580.  *  Return      :  0 if OK
  581.  *                 errno if error
  582.  *
  583.  *  OS/Compiler :  All
  584.  ***/
  585.  
  586. EXTERN int filetrunc( int fd, unsigned long length );
  587.  
  588.  
  589.  
  590.  
  591. /***
  592.  *  Function    :  Filetrunc
  593.  *
  594.  *  Description :  Truncate a file to a specified length.
  595.  *
  596.  *  Parameters  :  in   char *         path      file descriptor
  597.  *                 in   unsigned long  length    number of bytes to copy
  598.  *
  599.  *  Return      :  0 if OK
  600.  *                 errno if error
  601.  *
  602.  *  OS/Compiler :  All
  603.  ***/
  604.  
  605. EXTERN int Filetrunc( const char *path, int length );
  606.  
  607.  
  608.  
  609.  
  610. /***
  611.  *  Functions   :  filedel
  612.  *
  613.  *  Description :  Like 'unlink' but allows wildcards
  614.  *
  615.  *  Parameters  :  in   char *  filename   pathname with/without wildcards
  616.  *
  617.  *  Return      :  none
  618.  *
  619.  *  OS/Compiler :  MS-DOS
  620.  ***/
  621.  
  622. EXTERN void filedel( const char *filename );
  623.  
  624.  
  625.  
  626. /***
  627.  *  Function    :  getPasswd
  628.  *
  629.  *  Description :  Input a password (echoes '*')
  630.  *
  631.  *  Parameters  :  in  char *passwd    string to store password
  632.  *
  633.  *  Decisions   :  Echoes '*' in place of characters.
  634.  *                  Backspace and left arrow can be used to erase characters
  635.  *                  Characters must be in the range 32 - 254
  636.  *
  637.  *  Return      :  pointer to password
  638.  *
  639.  *  OS/Compiler :  MS-DOS & Turbo-C
  640.  ***/
  641.  
  642. EXTERN char *getPasswd( char *passwd );
  643.  
  644.  
  645.  
  646. /***
  647.  *  Function    :  vollabel
  648.  *
  649.  *  Description :  Get/Set disk volume label
  650.  *
  651.  *  Parameters  :  in       int drive         0 = A:, 1 = B:, ...
  652.  *                 in/out   char *vol         volume label
  653.  *
  654.  *  Decisions   :  - If *vol = '\0', it is filled with disk volume label.
  655.  *                 - Otherwise, vol is copied onto disk volume label.
  656.  *
  657.  *  Return      :   0 if OK
  658.  *                 -1 if error
  659.  ***/
  660.  
  661. EXTERN int vollabel( int drive, char *vol );
  662.  
  663.  
  664. /***     For Borland Only     ***/
  665.  
  666. EXTERN void beep( void );
  667. EXTERN void buzzer( void );
  668.  
  669.  
  670. #endif
  671.